package com.bwhx.eruler.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Version;
import com.bwhx.eruler.service.VersionService;
/**
 * @author Cuilitang
 *
 */

@Controller
public class VersionController extends BaseController {
	
	@Autowired
	VersionService verService;
	
	@ResponseBody
	@RequestMapping(value = "/page.verList.ashx")
	 public Map verList(Integer page,Integer rows,Version v) {
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,v);
		Map map = new HashMap();
		try{
			List<Version> list = verService.list(p);
			Integer count = verService.count(v);
			log.info(list);
			log.info(count);
			
			map.put("total",count );//total键 存放总记录数，必须的  
			map.put("rows",list);//rows键 存放每页记录 list 
			for (Version ver : list) {
				log.info(ver);
			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			map.put("meg", "失败！");
			map.put("status", "0");
		}
		
	    return map;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.saveOrUpdateVersion.ashx")
	public String saveOrUpdateVersion(Version ver) {
		try{
			if(ver.getvId()==-1){
				List<Version> list = verService.findByName(ver.getvName());
				if(list.size()!=0){return "conflict";}
				verService.insert(ver);
			}else{
				log.info("修改ver成功");
				verService.update(ver);
			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	
	@ResponseBody
	//@RequestMapping(value = "/page.get.ashx")
	 public Version get(Integer dId) {
		
		Version d = verService.get(dId);
	    return d;  
	 }
	
	
	@ResponseBody
	@RequestMapping(value = "/page.verListAll.ashx")
	 public List<Version>  verListAll() {
		
		List<Version> ld = verService.verListAll();
	    return ld;  
	 }
	
	
	
	@ResponseBody
	@RequestMapping(value = "/newestVersion.ashx")
	 public HashMap<String,String>  verList() {
		
		
		HashMap<String,String> resultMap = new HashMap<>();
		
		resultMap.put("status", "1");
		resultMap.put("msg", "获取成功");
		try{
			List<Version> verListAll = verService.verListAll();
			for (Version version : verListAll) {
				resultMap.put(version.getvName(), version.getvValue());
			}
		}catch(Exception e){
			resultMap.put("status", "0");
			resultMap.put("msg", "获取失败");
			log.error(getTrace(e));
		}
		return resultMap;
	 }
	

	

}
